<?php


namespace app\diy\controller;


use app\AdminRest;
use app\card\model\DefaultSetting;
use app\diy\model\DiyDataModel;
use app\diy\model\DiyModel;
use longbingcore\permissions\Tabbar;

class DiyController extends AdminRest
{

    public function list()
    {
        //初始化diy数据
        longbingInitDiy($this->_uniacid);

        $param = $this->_param;
        $m_diy = new DiyModel();
        $list = $m_diy->field(['status', 'diy_name', 'id', 'create_time', 'is_system'])
            ->where([['uniacid', '=', $this->_uniacid]])
            ->paginate(['list_rows' => $param['page_count'] ? $param['page_count'] : 10, 'page' => $param['page'] ? $param['page'] : 1])->toArray();

        return $this->success($list);
    }
    
    public function initOne()
    {
        $diy_name = $this->_input['diy_name'];

        $diy = new DiyModel();
        $diy->diy_name = $diy_name;
        $diy->uniacid = $this->_uniacid;

        $tabbars = longbingGetAppTabbar($this->_uniacid, true);
        $tabbars = longbingGetWxAppTabbarResponse($tabbars);

        $tabbars_list = $tabbars['list'];
        foreach ($tabbars_list as $key => $tabbar) {
            if (!in_array($tabbar['key'], [1, 3,  20, 21])) {
                unset($tabbars_list[$key]);
            }
        }

        $tabbars['list'] = array_values($tabbars_list);

        $diy->tabbar = json_encode($tabbars);

        $keys = array_column($tabbars['list'], 'key');

        $page_init = [];
        foreach ($keys as $key) {
            $page_init[$key] = ['list' => []];
        }

        $diy->page = json_encode($page_init);


        $rst = $diy->save();
        if ($rst) {
            return $this->success($diy->id);
        }

        return $this->error(false);
    }

    public function getDiyData()
    {
        $diy_id = $this->_param['diy_id'] ?? null;

        //没有处理后台老版本和新版本数据兼容处理的问题  By.jingshuixian
        $diy_data = DiyModel::where([['id', '=', $diy_id]])->find();

        if (empty($diy_data) ||  $diy_data->isEmpty()) {
            return $this->error('没有值');
        }

        $diy_data['page']   = json_decode($diy_data['page'], true);

        $diy_data['tabbar'] = json_decode($diy_data['tabbar'], true);

//        $diy_data['tabbar'] = array_values($diy_data['tabbar']);

        return $this->success($diy_data);
    }


    public function setDiyData()
    {
        $id = $this->_input['id'];
        $page =  $this->_input['page'] ?? null;
        $tabbar =  $this->_input['tabbar'] ?? null;
        $diy_name = $this->_input['diy_name'] ?? null;

        /**
         * @var DiyModel $diy_data
         */
        $diy_data = DiyModel::where([['uniacid', '=', $this->_uniacid], ['id', '=', $id]])->findOrEmpty();

        if ($diy_data->isEmpty()) {
            return $this->error('参数错误');
        } else {
            $save_data = null;
            if ($page ) $save_data['page'] = json_encode($page);
            if ($tabbar ) $save_data['tabbar'] = json_encode($tabbar);
            if ($diy_name ) $save_data['diy_name'] = $diy_name;
            if ($save_data) $rst = $diy_data->save($save_data);

            return $this->success($diy_data->id);
        }
    }


    public function delDiy()
    {
        $id = $this->_input['id'];
        /**
         * @var DiyModel $diy_data
         */
        $diy_data = DiyModel::where([['uniacid', '=', $this->_uniacid], ['id', '=', $id], ['is_system', '<>', 1]])->findOrEmpty();

        if ($diy_data->isEmpty()) {
            return $this->error('非法操作');
        } else {
            $rst = $diy_data->delete();
            return $this->success($diy_data->id);
        }

    }

    public function openADiy()
    {
        $diy_id = $this->_input['diy_id'] ?? null;

        $all_diys = DiyModel::where(['status' => 1,'uniacid'=>$this->_uniacid])->select();
        foreach ($all_diys as $diy) {
            $diy->save(['status' => 0]);
        }

        $open_diy = DiyModel::find($diy_id);
        $rst =  $open_diy->save(['status' => 1]);
        return $this->success($rst);

    }


    public function getSystemDefaulDiy()
    {


        /*//tabbar
        $rst = [];
        $tabbars = longbingGetAppTabbar($this->_uniacid, true);
        $tabbars = longbingGetWxAppTabbarResponse($tabbars);
        $tabbars_list = $tabbars['list'];
        foreach ($tabbars_list as $key => $tabbar) {
            if (!in_array($tabbar['key'], [1, 3,  20, 21])) {
                unset($tabbars_list[$key]);
            }
        }
        $tabbars['list'] = array_values($tabbars_list);
        $rst['tabbar'] = $tabbars;

        //page
        $keys = array_column($tabbars['list'], 'key');
        $page_init = [];
        foreach ($keys as $key) {
            $page_init[$key] = ['list' => []];
        }
        $rst['page'] = $page_init;*/


//        $default_data = config('app.AdminModelList')['diy_default_data'];


        $data = Tabbar::allForDiySelect($this->_uniacid);


        $data = array_column($data['list'],'key');


        $default_data['page']   = Tabbar::getAuthDefultPage($data);

        $default_data['tabbar'] = Tabbar::getAuthDefultTabbar($data);

        return $this->success($default_data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-28 14:45
     * @功能说明:获取配色
     */
    public function getDiyColor(){

        $data = [

            [
                'primaryColor'=> '#19c865',

                'subColor'    => '#f86c53'

            ],
            [
                'primaryColor'=> '#db4d4d',

                'subColor'    => '#e28e5f'

            ],
            [
                'primaryColor'=> '#dd6630',

                'subColor'    => '#e39a32'

            ],
            [
                'primaryColor'=> '#ebcc3b',

                'subColor'    => '#20252d'

            ],
            [
                'primaryColor'=> '#8ac4aa',

                'subColor'    => '#232826'

            ],
            [
                'primaryColor'=> '#8228f9',

                'subColor'    => '#b49fe2'

            ],
            [
                'primaryColor'=> '#ca2432',

                'subColor'    => '#efd645'

            ],
            [
                'primaryColor'=> '#db4d4d',

                'subColor'    => '#555555'

            ],
            [
                'primaryColor'=> '#85c077',

                'subColor'    => '#b1dda7'

            ],
            [
                'primaryColor'=> '#baaa6f',

                'subColor'    => '#dcd5a9'

            ],

        ];

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-29 19:06
     * @功能说明:正在被使用到颜色
     */
    public function useringColor(){

        $input = $this->_input;

        $config_model = new DefaultSetting();

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $config = $config_model->settingInfo($dis,'primaryColor,subColor');

        if(empty($config['primaryColor'])){

            $config['primaryColor'] = '#19c865';

            $config['subColor']     = '#f86c53';

        }

        return $this->success($config);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-28 17:22
     * @功能说明:保存配置颜色
     */
    public function saveDiyColor(){

        $input = $this->_input;

        $config_model = new DefaultSetting();

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $config = $config_model->settingInfo($dis);

        $data =[

            'primaryColor' => $input['primaryColor'],

            'subColor'     => $input['subColor'],
        ];

        $res   = $config_model->settingUpdate($dis,$data);

        return $this->success($res);

    }




}